hwd=$PWD

rm -rf source_vmess json_files
bash vmess_getlink.sh

python3 clean.py
python3 get_links.py
rm -rf json_files
mkdir json_files
cd json_files
cat ../source_vmess/source_vmess | python3 ../vmess2json/vmess2json.py --parse_all
# now you randomly pick a shit or you rename them all instead?
# use python one-liner instead.
python3 -c "import os;files=os.listdir('.');[os.rename(files[x],'{}.json'.format(x)) for x in range(len(files))]"
# now verify the targets.
ls .
# randomly pick up one or just use a fixed number?
tcnts=$(ls -1 | wc -l)
echo total $tcnts targets.
rndfile==$(python3 -c "import random;print(random.randint(0,$tcnts-1),end='')")
rndfile="$rndfile.json"
# just execute the thing here? don't prepare some directory for log exporting?
cd ..
rm -rf vmess_run_dir
mkdir vmess_run_dir
cd vmess_run_dir

cp /etc/v2ray/geoip.dat /usr/bin/geoip.dat
cp /etc/v2ray/geosite.dat /usr/bin/geosite.dat

v2ray -config ../json_files/1.json -format json -test
#echo ==============================================
#cat ../json_files/1.json
# could you use xargs?
# nope
cd $hwd

for ((i=0;i<$tcnts;i++)); 
do 
	target_id=$i
	echo testing target $target_id
	sleep 1
	target_ip=$(cat json_files/$target_id.json | python3 get_server_ip.py outbounds 0 settings vnext 0 address)
	target_port=$(cat json_files/$target_id.json | python3 get_server_ip.py outbounds 0 settings vnext 0 port)

	echo about to ping target at $target_ip:$target_port
	#v2ctl tlsping $target_ip --ip $target_ip
	test_counts=3
	target_delay=$(bash server_ping_test.sh $target_ip $test_counts)
	echo target delay: $target_delay
done

